#include "EEventShape.h"
#include "EEntity.h"
#include "EPhScene.h"

#include "eCPP.h"
namespace engine 
{
//------------------------------------------------------------
// ERegisteredEntityData
//------------------------------------------------------------

EEventShape::ERegisteredEntityData::ERegisteredEntityData() :
m_pEntity( NULL ),
m_pUserData( NULL ),
m_eState( EEventShape::REGISTERED_ENTITY_STATE_OUTSIDE )
{

}

//------------------------------------------------------------
EEventShape::ERegisteredEntityData::ERegisteredEntityData( EEntity& entity, void *pUserData) :
m_pEntity( &entity ),
m_pUserData( pUserData ),
m_eState( EEventShape::REGISTERED_ENTITY_STATE_OUTSIDE )
{

}

//------------------------------------------------------------
void EEventShape::ERegisteredEntityData::operator= (const ERegisteredEntityData& data)
{
  m_pEntity     = data.m_pEntity;
  m_pUserData   = data.m_pUserData;
  m_eState      = data.m_eState;
}
//------------------------------------------------------------
// EEventShape
//------------------------------------------------------------
EEventShape::EEventShape(const EBoxShape& shape, flag16 flagActivatedEventTypes ) :
m_flagShapeEventTypes( flagActivatedEventTypes ),
m_pPhActor( NULL ),
m_Shape( shape )
{
  cASSERT( flagActivatedEventTypes != 0 );
}

//------------------------------------------------------------
EEventShape::~EEventShape()
{
  m_mapEntities.clear();
}

//------------------------------------------------------------
void  EEventShape::setEnabledShapeEventType( _eShapeEventType eEventType, bool bEnabled )
{
  if( bEnabled )
    m_flagShapeEventTypes |= eEventType;
  else
    m_flagShapeEventTypes &= ~eEventType;

  cASSERT( m_flagShapeEventTypes != 0 );
}

//------------------------------------------------------------
bool EEventShape::isShapeEventTypeEnabled(_eShapeEventType eEventType )
{
  return ( m_flagShapeEventTypes & eEventType ) != 0;
}

//------------------------------------------------------------
void EEventShape::registerEntity(EEntity& entity, void *pUserData)
{
  cASSERT_STRING( m_mapEntities.find( &entity ) == m_mapEntities.end(), "an entity is already registered in the event shape" );
  m_mapEntities[ &entity ] = ERegisteredEntityData( entity, pUserData );
}

//------------------------------------------------------------
void EEventShape::unregisterEntity(EEntity& entity )
{
  m_mapEntities.erase(&entity);
}

//------------------------------------------------------------
EPhActor& EEventShape::createActor(EPhScene &phScene)
{
  EPhShapeDesc shapeDesc( m_Shape );

  // Set activated events
  if( isShapeEventTypeEnabled( EEventShape::SHAPE_EVENT_ENTER ) )
    shapeDesc.m_flagFlags |= NX_TRIGGER_ON_ENTER;
  if( isShapeEventTypeEnabled( EEventShape::SHAPE_EVENT_LEAVE ) )
    shapeDesc.m_flagFlags |= NX_TRIGGER_ON_LEAVE;
  if( isShapeEventTypeEnabled( EEventShape::SHAPE_EVENT_IS_STILL_INSIDE ) )
    shapeDesc.m_flagFlags |= NX_TRIGGER_ON_STAY;

  shapeDesc.m_eCollisionGroup = COLLISION_GROUP_EVENTS;

  EPhActorDesc actorDesc;
  actorDesc.addShape( shapeDesc );
  m_pPhActor = phScene.createActor( actorDesc );
  cASSERT( m_pPhActor != NULL );
  return *m_pPhActor;
}

//------------------------------------------------------------
void EEventShape::destroyActor(EPhScene& phScene )
{
  cASSERT( m_pPhActor != NULL );
  phScene.destroyActor( *m_pPhActor );
  m_pPhActor = NULL;
}

/*//------------------------------------------------------------
// EBoxEventShape
//------------------------------------------------------------
EBoxEventShape::EBoxEventShape(flag16 flagActivatedEventTypes, const EVector& vDimensions ) :
EBoxShape( vDimensions ),
EEventShape( flagActivatedEventTypes )
{

}*/

} // end namespace